<template>
    <el-dialog
        title="添加设备"
        :visible.sync="showValue"
        append-to-body
        :close-on-click-modal="false"
        width="900px"
        top="30px"
        :close="clearData"
        :before-close="clearData"
    >
        <div>
            <el-form
                ref="form"
                size="small"
                inline
                :model="form"
                label-width="80px"
            >
                <el-form-item label="经营类型:" prop="managementType">
                    <el-select
                        class="item"
                        v-model="form.managementType"
                        placeholder="请选择"
                        size="small"
                    >
                        <el-option label="全部" value></el-option>
                        <el-option
                            v-for="(item, index) in $store.state.typeList"
                            :key="index"
                            :label="item.managementTypeName"
                            :value="item.managementType"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="商家编码:" prop="companyCode">
                    <el-input
                        v-model="form.companyCode"
                        class="item"
                    ></el-input>
                </el-form-item>
                <el-form-item label="商家名称:" prop="companyName">
                    <el-input
                        v-model="form.companyName"
                        class="item"
                    ></el-input>
                </el-form-item>
                <el-form-item label="SN码:" prop="chipID">
                    <el-input v-model="form.chipID" class="item"></el-input>
                </el-form-item>
                <el-button type="primary" size="small" @click="getList"
                    >查询</el-button
                >
                <el-button
                    type="primary"
                    size="small"
                    plain
                    @click="resetFields"
                    >重置</el-button
                >
                <el-button type="primary" size="small" plain @click="allAdd"
                    >批量添加</el-button
                >
            </el-form>
            <el-table
                v-loading="loading"
                :header-cell-style="headerCellStyle"
                :data="tableData"
                @selection-change="handleSelectionChange"
                size="small"
                max-height="350px"
            >
                <el-table-column type="selection"></el-table-column>
                <el-table-column
                    prop="managementTypeName"
                    label="经营类型"
                    show-overflow-tooltip
                ></el-table-column>
                <el-table-column
                    prop="companyCode"
                    label="商家编号"
                    show-overflow-tooltip
                ></el-table-column>
                <el-table-column
                    prop="companyName"
                    label="商家名称"
                    show-overflow-tooltip
                >
                </el-table-column>
                <el-table-column
                    prop="chipID"
                    label="SN码"
                    show-overflow-tooltip
                >
                </el-table-column>
                <el-table-column fixed="right" label="操作" width="80px">
                    <template slot-scope="scope">
                        <el-button
                            size="small"
                            type="primary"
                            @click="addCompany([scope.row])"
                            >添加</el-button
                        >
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div style="width: 100%; padding: 15px" class="right">
            <el-pagination
                background
                :current-page.sync="currentPage"
                @current-change="handleCurrentChange"
                :page-size="pageSize"
                layout="prev, pager, next, total"
                :total="totalNum"
            ></el-pagination>
        </div>
    </el-dialog>
</template>

<script>
export default {
    props: {
        value: Boolean,
        currRow: Object
    },
    data () {
        return {
            currentPage: 1,
            pageSize: 10,
            totalNum: 0,
            typeList: [],
            loading: false,
            tableData: [],
            checkArr: [],
            showValue: false,
            form: {
                chipID: '',
                companyCode: '',
                companyName: '',
                managementType: ''
            }
        }
    },
    watch: {
        value (val) {
            console.log(val)
            this.showValue = val
        },
        showValue (val) {
            if (val) {
                this.getList()
            }
            this.$emit('update:value', val)
        }
    },
    methods: {
        handleSelectionChange (arr) {
            this.checkArr = arr
        },
        allAdd () {
            if (this.checkArr.length === 0) {
                return this.$message({
                    message: '请先勾选账户',
                    type: 'warning'
                })
            }
            this.addCompany(this.checkArr)
        },
        addCompany (arr) {
            let objs = arr.map(item => {
                return {
                    sn: item.chipID,
                    companyCode: item.companyCode,
                    companyName: item.companyName
                }
            })
            this.$confirm('确定添加吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$http.post(this.$store.state.IP + '/upgradeManage/addUpgradeTask', {
                    listUpgradeTaskBaseInfo: objs,
                    upgradePackageID: this.currRow.upgradePackageID,
                    upgradeStage: 0
                },
                    {
                        'emulateJSON': false
                    }
                ).then(response => {
                    this.loading = false
                    console.log(response)
                    response = response.body
                    if (response.ret === '0') {
                        this.$message({
                            message: '操作成功',
                            type: 'success'
                        })
                        this.getList()
                    }
                }, response => {
                    this.loading = false
                    console.log('通讯失败')
                })
            }).catch(() => {
            })
        },
        handleCurrentChange (page) {
            // 换页
            this.currentPage = page
            this.getList()
        },
        getList () {
            this.$http.post(this.$store.state.IP + '/upgradeManage/getTerminalPage', {
                ...this.form,
                page: this.currentPage - 1, // 页数
                size: 10, // 一页显示数量
                terminalType: this.currRow.terminalType,
                status: 2,
                upgradePackageID: this.currRow.upgradePackageID,
                upgradeStage: 0
            },
                {
                    'emulateJSON': false
                }
            ).then(response => {
                response = response.body
                if (response.ret === '0') {
                    this.tableData = response.res.list
                    this.totalNum = response.res.total
                }
                if (response.ret !== '0') {
                    console.log('调用设备报表失败')
                }
                console.log('******************************************')
            }, response => {
                console.log('通讯失败')
            })
        },
        resetFields () {
            this.currentPage = 1
            this.$refs['form'].resetFields()
            this.getList()
        },
        // 清空数据
        clearData () {
            this.$refs['form'].resetFields()
            this.$emit('init')
            this.currentPage = 1
            this.checkArr = []
            this.showValue = false
        }
    }
}
</script>
<style lang="stylus" rel="stylesheet/stylus" scoped>
.item
    width: 180px

.right
    text-align: right
</style>